Skip to main content

Control statements

Conditional statements

if statement

If the Boolean expression is true, the block of code inside the if statement will be executed. If the Boolean expression is false, the first block of code after the end of the if statement (after the closing brackets) will be executed.

C assumes any non-zero and non-null values to be true and zero or null to be false.

if (boolean_expression)
{
// statement that will be executed if the boolean expression is true
}

// Instance
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
int a = 10;

// Check the boolean condition with the if statement
if( a < 20 )
{
// If the condition is true, output the following statement
cout << "a is less than 20" << endl;
}
cout << "The value of a is " << a << endl;

return 0;
}

// Output
a is less than 20
The value of a is 10

if... . .else statement

If the Boolean expression is true, the code in the if block is executed. If the boolean expression is false, the code in the else block is executed.

if (boolean_expression)
{
// The statement to be executed if the boolean expression is true
}
else
{
// statement to be executed if the boolean expression is false
}

// Instance
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
int a = 100;

// Check the boolean condition
if( a < 20 )
{
// If the condition is true, output the following statement
cout << "a is less than 20" << endl;
}
else
{
// If the condition is false, the following statement is output
cout << "a is greater than 20" << endl;
}
cout << "The value of a is " << a << endl;

return 0;
}

// Output
a is greater than 20
The value of a is 100

if...else if .else if Multi-branch statements

An if statement can be followed by an optional else if....else statement. .else** statement, which can be used to test a variety of conditions.

When using if.... .else if.... .else statements, the following points should be noted.

  • An if can be followed by zero or one else, and else must follow all else ifs.
  • An if may be followed by zero or more else ifs, and else ifs must precede else.
  • Once an else if is matched, the other else ifs or else will not be tested.
// Syntax
if(boolean_expression 1)
{
// executed when boolean expression 1 is true
}
else if( boolean_expression 2)
{
// executed when boolean expression 2 is true
}
else if( boolean_expression 3)
{
// executed when boolean expression 3 is true
}
else
{
// executed when none of the above conditions are true
}

// Instance
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
int a = 100;

// Check the boolean condition
if( a == 10 )
{
// If the if condition is true, output the following statement
cout << "The value of a is 10" << endl;
}
else if( a == 20 )
{
// If the else if condition is true, the following statement is output
cout << "The value of a is 20" << endl;
}
else if( a == 30 )
{
// If the else if condition is true, the following statement is output
cout << "The value of a is 30" << endl;
}
else
{
// If none of the above conditions are true, the following statement is output
cout << "There is no matching value" << endl;
}
cout << "The exact value of a is " << a << endl;

return 0;
}

// Output
No matching value
The exact value of a is 100

switch multi-branch statement

A switch statement allows a variable to be tested when it is equal to more than one value. Each value is called a case, and the variable being tested is checked for each switch case.

syntax

switch(expression){
case constant-expression :
statement(s);
break; // optional
case constant-expression :
statement(s);
break; // optional

// You can have any number of case statements
default : // optional
statement(s);
}

The switch statement must follow the following rules.

  • The expression in a switch statement must be an integer or enumerated type, or a class type where the class has a single conversion function to convert it to an integer or enumerated type.
  • There can be any number of case statements in a switch. Each case is followed by a value to be compared and a colon.
  • The constant-expression of a case must be of the same data type as the variable in the switch, and must be a constant or literal.
  • When the variable being tested is equal to the constant in the case, the statement following the case is executed until a break statement is encountered.
  • When a break statement is encountered, the switch terminates and the control flow jumps to the next line after the switch statement.
  • Not every case needs to contain break. If a case statement does not contain a break, the control flow will continue with subsequent cases until a break is encountered.
  • A switch statement can have an optional default case that appears at the end of the switch. default case can be used to execute a task if none of the above cases are true. the break statement in default case is not required.

flowchart

图例

Instance

// Instance
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
char grade = 'D';

switch(grade)
{
case 'A' :
cout << "Great!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;

return 0;
}

// Output
You have passed
Your grade is D

Loop statement

while loop statement

// Syntax
while(condition)
{
statement(s);
}

// Instance
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
int a = 10;

// while loop execution
while( a < 20 )
{
cout << "Value of a:" << a << endl;
a++;
}

return 0;
}

// Output
Value of a: 10
Value of a: 11
Value of a: 12
a value: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
The value of a: 18
The value of a: 19

do.... ...while loop statement

do.... while loop is similar to the while loop, but the do.... while loop will ensure that the loop is executed at least once.

// Syntax
do
{
statement(s);

}while( condition );

// Instance
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
int a = 10;

// do loop execution
do
{
cout << "Value of a:" << a << endl;
a = a + 1;
}while( a < 20 );

return 0;
}

// Output
Value of a: 10
Value of a: 11
Value of a: 12
a value: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
The value of a: 18
The value of a: 19

for loop statements

The for loop allows you to write a repetitive control structure that executes the loop a specific number of times

  1. init will be executed first and will only be executed once. This step allows you to declare and initialise any loop control variables. You can also write no statements here, as long as a semicolon appears. 2.
  2. Next, condition will be determined. If it is true, the body of the loop is executed. If false, the body of the loop is not executed and the control flow jumps to the next statement immediately following the for loop. 3.
  3. After the for loop body is executed, the control flow jumps back to the increment statement above. This statement allows you to update the loop control variable. The statement can be left blank, as long as a semicolon appears after the condition.
  4. The condition is judged again. If it is true, the loop is executed and the process is repeated (the body of the loop, then the step value is added, then the condition is re-judged). At the point where the condition becomes false, the for loop terminates.
for ( init; condition; increment )
{
statement(s);
}
#include <iostream>
using namespace std;

int main ()
{
// for loop execution
for( int a = 10; a < 20; a = a + 1 )
{
cout << "Value of a:" << a << endl;
}

return 0;
}

// Output
Value of a: 10
Value of a: 11
Value of a: 12
a value: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
The value of a: 18
The value of a: 19

Jump statements

The break statement

  1. When the break statement appears within a loop, the loop terminates immediately and the program flow continues with the next statement immediately following the loop. 2.
  2. It can be used to terminate a case in a switch statement.
break;
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
int a = 10;

// do loop execution
do
{
cout << "Value of a:" << a << endl;
a = a + 1;
if( a > 15)
{
// stop the loop
break;
}
}while( a < 20 );

return 0;
}

The continue statement

The continue statement in C++ is a bit like the break statement. But instead of forcing a termination, continue skips the code in the current loop and forces the next one to start.

continue;
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
int a = 10;

// do loop execution
do
{
if( a == 15)
{
// skip the iteration
a = a + 1;
continue;
}
cout << "Value of a:" << a << endl;
a = a + 1;
}while( a < 20 );

return 0;
}

The goto statement

The goto statement allows control to be transferred unconditionally to a marked statement within the same function.

The goto statement is not recommended in any programming language. It makes the control flow of the program difficult to follow, making it difficult to understand and to modify.

goto label;
...
.
label: statement;
#include <iostream>
using namespace std;

int main ()
{
// local variable declaration
int a = 10;

// do loop execution
LOOP:do
{
if( a == 15)
{
// skip the iteration
a = a + 1;
goto LOOP;
}
cout << "Value of a:" << a << endl;
a = a + 1;
}while( a < 20 );

return 0;
}

return statement

The return statement